import { dengLuAPI, getuserinfoAPI } from '@/api/login' //
import router from '@/router'
import { Message } from 'element-ui'

export default {
  namespaced: true,

  state: {
    token: '',
    userinfo: ''
  },
  mutations: {
    getLoginToken(state, token) {
      state.token = token
    },
    removeToken(state) {
      state.token = ''
    },
    userinfo(state, userinfo) {
      state.userinfo = userinfo
    },
    removeUserinfo(state) {
      state.userinfo = ''
    }
  },
  actions: {
    async loginActions(context, data) {
      try {
        const res = await dengLuAPI(data)
        console.log(res)
        if (res.data.msg === '登录成功') {
          // router.push('/')
          context.commit('getLoginToken', res.data.token)
          const { data } = await getuserinfoAPI(res.data.userId)
          console.log(data)
          router.push('/')
        } else {
          Message.error(res.data.msg)
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
}
// 登录页面
